#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <errno.h>

int main(int argc,char **argv)
{
    int fd = 0;
    char val = 0;
    int flags = 0;
    int ret = 0;

    fd = open("/dev/eint_key", O_RDONLY);
    if (fd < 0) {
        printf("open failed \n");
        return -1;
    }

    /*如何设置文件为非阻塞  
    1.读取flags  2.flags |= O_NONBLOCK;  3.写会去
    */
    flags = fcntl(fd, F_GETFL ,0);
    flags |= O_NONBLOCK;
    fcntl(fd, F_SETFL, flags);

    /*read 一个非阻塞的文件, 如果当前没有数据
    会立马返回,ret = -1,同时 errno == EAGAIN
    */
    while (1) {
        ret = read(fd, &val, 1);
        if (ret < 0) {
            if (errno == EAGAIN) {
                printf("read again\n");
                sleep(2);
            }
        } else {
            printf("val = %x\n", val);
        }  
    }

    close(fd);

    return 0;
}